package com.jinxinol.basic.system.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;

import com.jinxinol.basic.system.domain.SysFastmenu;
import com.jinxinol.basic.system.domain.SysRight;
import com.jinxinol.basic.system.service.ISysFastmenuService;
import com.jinxinol.basic.system.service.ISysRightService;
import com.jinxinol.core.common.BaseAction;
import com.jinxinol.core.common.ContextHelper;

/**
 * 
 * @company Jinxin Computer Corp.
 * @author LXD
 * @email 247428412@qq.com
 * @description 系统主页内容（快捷菜单）
 * @date 2012-09-05
 */
public class SysMainAction extends BaseAction {
	private static final long serialVersionUID = -8004959989512432543L;
	private static final Log logger = ContextHelper.getLogger(SysMainAction.class);
	
	private List<SysFastmenu> userfastList;
	private List<SysRight> fastList;//用户定义的快捷栏目
	private List<SysRight> userrightList; // 用户的菜单
	private Map<SysRight, List<SysRight>> showMap; // 自定义页面显示内容
	
	@Resource ISysFastmenuService sysFastmenuService;
	@Resource ISysRightService sysRightService;
	
	/**
	 * 用户自定义快捷栏目菜单
	 * */
	public String insertFastMenu() throws Exception{
		try {
			sysFastmenuService.deleteUserRight(sysUser.getId());//删除以前设置
			if (userfastList!=null) {
				for (int i = 0; i < userfastList.size(); i++) {
					userfastList.get(i).setUser_id(sysUser.getId());
					sysFastmenuService.insert(userfastList.get(i));
				}
			}
			return "customMenu";
		} catch (Exception e) {
			throw new  Exception("用户自定义快捷栏目菜单错误！\n详细日志："+e.getMessage());
		}
	}
	
	/**
	 * 用户快捷菜单展示（权限菜单展示）
	 * */
	public String customMenu() throws Exception{
		try {
			List<SysRight> tempuserrightList = sysUser.getUserRight();//用户所有的
			//userrightList = sysUser.getUserRight();//用户所有的
			fastList = sysFastmenuService.getUserRight(sysUser.getId());//用户已经定义好的
			
			if (tempuserrightList!=null) {
				for (int h = 0; h < tempuserrightList.size(); h++) {
					if (tempuserrightList.get(h).getCode() != null && !tempuserrightList.get(h).getR_type().equals(2) ) {
						for (int i = 0; i < fastList.size(); i++) {
							if (tempuserrightList.get(h).getId().equals(fastList.get(i).getId())) {
								tempuserrightList.get(h).setChecded(1);//已经选中
							}
						}
						userrightList.add(tempuserrightList.get(h));
					}
				}
			}

			if (userrightList != null && userrightList.size() > 0) {// 存在可以遍历的菜单
				showMap = new HashMap<SysRight, List<SysRight>>();
				List<SysRight> genList = new ArrayList<SysRight>();
				List<SysRight> twoList = new ArrayList<SysRight>();
				List<SysRight> thrList = new ArrayList<SysRight>();
				for (int i = 0; i < userrightList.size(); i++) {// 遍历各级菜单
					SysRight xRight = userrightList.get(i);
					if (xRight.getLevel_() == 1) {
						genList.add(xRight);
						showMap.put(xRight, new ArrayList<SysRight>());
					} else if (xRight.getLevel_() == 2) {
						twoList.add(xRight);
					} else {
						thrList.add(xRight);
					}
				}
				for (int j = 0; j < genList.size(); j++) {// 遍历出每个根菜单对应的第二级菜单
					for (int k = 0; k < twoList.size(); k++) {
						SysRight twoRight = twoList.get(k);
						if (twoRight.getParent_id() == genList.get(j).getId()&& twoRight.getCode() != null) {
							List<SysRight> xList = showMap.get(genList.get(j));
							xList.add(twoRight);
							showMap.put(genList.get(j), xList);
							twoList.remove(k);
							k--;
						}
					}
				}
				for (int m = 0; m < twoList.size(); m++) {// 遍历出每个二级菜单对应的第三级菜单
					SysRight genRight = null;
					for (int n = 0; n < thrList.size(); n++) {
						SysRight thrRight = thrList.get(n);
						if (thrRight.getParent_id().equals(twoList.get(m).getId())
								&& thrRight.getCode() != null) {
							for (int j = 0; j < genList.size(); j++) {
								if (genList.get(j).getId().equals(
										twoList.get(m).getParent_id())) {
									genRight = genList.get(j);
									break;
								}
							}
							List<SysRight> xList = showMap.get(genRight);
							xList.add(thrRight);
							showMap.put(genRight, xList);
						}
					}
				}
			}
			return "customMenu";
		} catch (Exception e) {
			throw new  Exception("用户权限菜单展示加载错误！\n详细日志："+e.getMessage());
		}
	}
	
	/**
	 * 主页内容加载（快捷菜单、）
	 * */
	public String sysInit() throws Exception{
		try {
			fastList=new ArrayList<SysRight>();
			fastList = sysFastmenuService.getUserRight(sysUser.getId());
			return "sysInit";
		} catch (Exception e) {
			throw new  Exception("主页内容加载错误！\n详细日志："+e.getMessage());
		}
	}

	public List<SysFastmenu> getUserfastList() {
		return userfastList;
	}

	public void setUserfastList(List<SysFastmenu> userfastList) {
		this.userfastList = userfastList;
	}

	public List<SysRight> getFastList() {
		return fastList;
	}

	public void setFastList(List<SysRight> fastList) {
		this.fastList = fastList;
	}

	public List<SysRight> getUserrightList() {
		return userrightList;
	}

	public void setUserrightList(List<SysRight> userrightList) {
		this.userrightList = userrightList;
	}

	public Map<SysRight, List<SysRight>> getShowMap() {
		return showMap;
	}

	public void setShowMap(Map<SysRight, List<SysRight>> showMap) {
		this.showMap = showMap;
	}
	
	
	
}
